package com.wnhz.work.jy.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

import com.wnhz.work.jy.BaseActivity;
import com.wnhz.work.jy.MyApplication;
import com.wnhz.work.jy.R;
import com.wnhz.work.jy.utils.ConfigUtil;
import com.wnhz.work.jy.utils.xutils3.MyCallBack;
import com.wnhz.work.jy.utils.xutils3.XUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * ---业绩管理
 */
public class PerformanceActivity extends BaseActivity implements View.OnClickListener {
    private static final String TAG = "PerformanceActivity";
    private TextView title;

    private String people;//推荐人数
    private String money;//推荐金额

    private TextView tv_people_number;//推荐人数
    private TextView tv_money;//推荐金额

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_performance);

        initView();
        getData();
    }

    private void setData() {
        tv_people_number.setText(people);
        tv_money.setText(money);
    }

    private void initView() {
        title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("业绩管理");
        findViewById(R.id.btn_main_left).setOnClickListener(this);
        findViewById(R.id.rl_my_performance).setOnClickListener(this);//我的业绩
        findViewById(R.id.rl_ranking).setOnClickListener(this);//排行榜
        findViewById(R.id.rl_generate).setOnClickListener(this);//生成订单
        tv_people_number = (TextView) findViewById(R.id.tv_people_number);
        tv_money = (TextView) findViewById(R.id.tv_money);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;

            //我的业绩
            case R.id.rl_my_performance:
                startActivity(new Intent(PerformanceActivity.this, MyPerformanceActivity.class));
                break;

            //排行榜
            case R.id.rl_ranking:
                startActivity(new Intent(PerformanceActivity.this, RankingListActivity.class));
                break;

            //生成订单
            case R.id.rl_generate:
                startActivity(new Intent(PerformanceActivity.this, GenerateOrderActivity.class));
                break;

            default:
                break;
        }
    }


    private void getData(){
        Map<String,Object> map = new HashMap<>();
        map.put("userId", MyApplication.getInstance().userBean.getUserId());
        showDialog();
        XUtil.Post(ConfigUtil.MANAGEMENT,map,new MyCallBack<String>(){
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.e(TAG, "onSuccess: "+result );
                try {
                    JSONObject json = new JSONObject(result);
                    people = json.optString("people");
                    money = json.optString("money");
                    setData();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                Log.e(TAG, "onError: "+ex);
            }

            @Override
            public void onFinished() {
                super.onFinished();
                closeDialog();
            }
        });
    }
}
